/*
 * Assignment.java
 *
 * Created on October 27, 2007, 10:30 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package MainClasses;

import java.util.*;
import MainClasses.FromAPCompSci.*;
/**
 * 
 *
 * @author andrewcutler
 */
public class Assignment {
    
    public static final int HW = 0;
    public static final int CW = 1;
    public static final int TEST = 2;
    public static final int PROJECT = 3;
    
    //private Date dateAssigned;
    //private Date dateDue;
    private Course classAssignedTo;
    private String title;
    private String assignmentText;
    private int type;
    private MyDate dateAssigned;
    private MyDate dateDue;
    private boolean checked;
    /** Creates a new instance of Assignment */
    public Assignment(int t) {
        type = t;
    }
    
    @Override
    public String toString()
    {
        String returnString = "";
        if(type == Assignment.CW)
        {
            returnString += "Classwork: ";
        }
        if(type == Assignment.HW)
        {
            returnString += "Homework: ";
        }
        if(type == Assignment.TEST)
        {
            returnString += "Test: ";
        }
        if(type == Assignment.PROJECT)
            returnString += "Project: ";
        
        returnString += title;
        return returnString;
    }
    
    public String getUniqueID()
    {
        return type+title+dateDue.toString(MyDate.FORMAT_3, "-")+dateAssigned.toString(MyDate.FORMAT_3, "-");
    }
    
    public boolean idsEqual(String s)
    {
        if(s.equals(getUniqueID()))
        {
            return true;
        }
        return false;
    }
    
    public  boolean isHomework()
    {
        if(getType() == Assignment.HW)
            return true;
        return false;
    }
    
    public  boolean isClasswork()
    {
        if(getType() == Assignment.CW)
            return true;
        return false;
    }
    
    public  boolean isTest()
    {
        if(getType() == Assignment.TEST)
            return true;
        return false;
    }
    
    public boolean isProject()
    {
        if(getType() == Assignment.PROJECT)
            return true;
        return false;
    }
    
    public int getType()
    {
        return type;
    }
    public void setTitle(String s)
    {
        title = s;
    }
    public void setCourse(Course c)
    {
        classAssignedTo = c;
    }
    
    public void check()
    {
        checked = !checked;
    }
    
    public boolean isChecked()
    {
        return checked;
    }
    public void setDateAssigned(MyDate c)
    {
        dateAssigned = c;
    }
    public void setDateDue(MyDate c)
    {
        dateDue = c;
    }
    public void setText(String s)
    {
        
        assignmentText = Assignment.removeForeignChars(s);
        
    }
    
    public String getTitle()
    {
        return title;
    }
    
    public MyDate getDateAssigned()
    {
        return dateAssigned;
    }
    
    public String getText()
    {
        return assignmentText;
    
    }
    
    public MyDate getDateDue()
    {
        return dateDue;
    }
    
    public Course getCourse()
    {
        return classAssignedTo;
    }
    private static void addForeignChar(MyMap<String, String> list)
    {
        list.put("&Agrave;","À");
        list.put("&agrave;","à");
        list.put("&Egrave;","È");
        list.put("&egrave;","è");
        list.put("&Igrave;","Ì");
        list.put("&igrave;","ì");
        list.put("&Ograve;","Ò");
        list.put("&ograve;","ò");
        list.put("&Ugrave;","Ù");
        list.put("&ugrave;","ù");
        
        list.put("&Aacute;","Á");
        list.put("&aacute;","á");
        list.put("&Eacute;","É");
        list.put("&eacute;","é");
        list.put("&Iacute;","Í");
        list.put("&iacute;","í");
        list.put("&Oacute;","Ó");
        list.put("&oacute;","ó");
        list.put("&Uacute;","Ú");
        list.put("&uacute;","ú");
        
        list.put("&Acirc;","Â");
        list.put("&acirc;","â");
        list.put("&Ecirc;","Ê");
        list.put("&ecirc;","ê");
        list.put("&Icirc;","Î");
        list.put("&icirc;","î");
        list.put("&Ocirc;","Ô");
        list.put("&ocirc;","ô");
        list.put("&Ucirc;","Û");
        list.put("&ucirc;","û");
        
        list.put("&Ccedil;","Ç");
        list.put("&ccedil;","ç");
        
        list.put("&Auml;","Ä");
        list.put("&auml;","ä");
        list.put("&Euml;","Ë");
        list.put("&euml;","ë");
        list.put("&Iuml;","Ï");
        list.put("&iuml;","ï");
        list.put("&Ouml;","Ö");
        list.put("&ouml;","ö");
        list.put("&Uuml;","Ü");
        list.put("&uuml;","ü");
        list.put("&#376;","Ÿ");
        list.put("&yuml;","ÿ");
        
        list.put("&Ntilde;","Ñ");
        list.put("&ntilde;","ñ");
        
        list.put("&laquo;", "<<");
        list.put("&raquo;", ">>");
        list.put("&lsquo;", "'");
        
        list.put("&rsquo;", "'");
        list.put("&ldquo;", "\"");
        list.put("&rdquo;", "\"");
        list.put("&iexcl;","¡");
        list.put("&iquest;","¿");
        list.put("&bull;", "\n ¬ ");
        list.put("&gt;", ">");
        list.put("&lt;", "<");
        list.put("&nbsp;", " ");
        list.put("<br />", "\n");
        list.put("<p>", " ");
        list.put("</p>", " ");
        
        
        
    }
    
    public static String removeForeignChars(String s)
    {
        MyMap<String, String> foreignCharacters = new MyMap<String,String>();
        addForeignChar(foreignCharacters);
        addEscapeSequences(foreignCharacters);
        ArrayList<MapEntry<String,String>> entries = foreignCharacters.getEntries();
        
        for(MapEntry<String,String> entry:entries)
        {
            String key = entry.getKey();
            while(s.indexOf(key) >= 0)
            {
                int keyLength = key.length();
                String firstPart = s.substring(0,s.indexOf(key));
                String secondPart = s.substring(s.indexOf(key)+keyLength);
                if(s.indexOf(key) == 0)
                    s = secondPart;
                else
                    s = firstPart + entry.getValue() + secondPart;
            }
            
        }
        
        return s;
    }
    
    private static void addEscapeSequences(MyMap<String, String> list)
    {
        list.put("\n", "<br /> ");
        list.put("\t", " ");
    }
                        
    
    public static String removeEscapeSequences(String s)
    {
        MyMap<String, String> chars = new MyMap<String, String>();
        addEscapeSequences(chars);
        ArrayList<MapEntry<String, String>> entries = chars.getEntries();
        
        for(MapEntry<String, String> entry:entries)
        {
            String key = entry.getKey();
            while(s.indexOf(key) >= 0)
            {
                int keyLength = key.length();
                String firstPart = s.substring(0, s.indexOf(key));
                String secondPart = s.substring(s.indexOf(key)+keyLength);
                if(s.indexOf(key) == 0)
                    s = secondPart;
                else
                    s = firstPart + entry.getValue() + secondPart;
            
            }
        }
        return s;
    }
    
    
}
